﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
using devtm.Aop.Extensions;
using devtm.Aop.Linq.Boxs;
using devtm.Aop.Convert;
using devtm.Aop.Event;

namespace devtm.Aop.Linq
{

    public class ProxyBuild<T> : Proxy<T>, IBuildSave
    {


        public ProxyBuild(Type type)
            : base(type)
        {
            
            // reporter les constructeurs
           
        }


        public void Transform(Mono.Cecil.TypeDefinition type)
        {

            Converter converter;
            converter = new Converter(type.Module);

            foreach (var group in this._list)
            {

                GroupMethod g = group.Value;


                InterceptorContainer box = new InterceptorContainer();
                foreach (var item in g.Interceptors)
                {
                    IInterceptor i = item.Value;
                    if (i.BeforeMethod != null)
                    {
                        MethodDefinition meth = type.Module.Import(i.BeforeMethod).Resolve();
                        box.Add(InterceptEnumEventType.BeforeCall, meth, null, 0, null);
                    }
                }



                
                List<MethodDefinition> methodes = new List<MethodDefinition>();
                foreach (MethodSign sign in g.Methods)
                {
                    var types = type.Module.Import(sign.Parameters);
                    MethodDefinition m = type.AsMethod(sign.MethodName, types.ToArray());
                    methodes.Add(m);

                    converter.ReplaceMethod(m, box);

                }

                

            }


            

        }


    }

}
